package com.chen.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class ChannelWriteTest {
    public static void main(String[] args) throws IOException {
        String text = "hello cyj";
        FileOutputStream fileOutputStream = new FileOutputStream("D:/a.txt");
        //实际上是实现类在起作用
        FileChannel channel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str放到buffer里面去
        byteBuffer.put(text.getBytes(StandardCharsets.UTF_8));
        byteBuffer.flip();//翻转
        //将buffer数据写到通道里面去
        channel.write(byteBuffer);
        fileOutputStream.close();

    }
}
